
function ShowHide(sObjID, sBefore, sAfter) {
	
	if ((sBefore == null) || (sAfter == null)) {
		var sBefore = 'item'
		var sAfter = 'item2'
	}	

	if (document.all)
		var showItem = document.all[sObjID]
	else
		var showItem = document.getElementById(sObjID)

	if ( showItem.className == sAfter) {
		showItem.className = sBefore;
	} else {
		showItem.className = sAfter;
	}
}

//poll related function
var ToShowPoll = 1

function SetNoShowPoll() {
	ToShowPoll = 0
}


//check whether passed parameter is a valid number
function notNumber(number) {
	number = number.toString()

	for (var i=0; i<number.length; i++) {
		if (number.charAt(i) > "9" || number.charAt(i) < "0") return true
	}

	return false
}


//adds digit grouping to the number
function formatNumber(number) {
	if (number.toString() == "") {
		if (typeof (Common_Scripts_FuncLib_OnRequest) == "undefined") 
			Common_Scripts_FuncLib_OnRequest = "On Request"

		return Common_Scripts_FuncLib_OnRequest
	}

	if (number < 0) {
		var sign = "-"
		number = -number
	} else{
		var sign = ""
	}
	
	number = number.toString()
	var tmpNumber = ""
	
	var dot = false
	for (var j=0; j<number.length; j++) {
		if (number.charAt(number.length - j - 1) == ".") {
			dot = true
			break
		}
	}

	var fraction
	if (dot) {
		fraction = number.substring(number.length - j - 1, number.length)
		number = number.substring(0, number.length - j - 1)
	}

	for (var i=0; i<number.length; i++) {		
		if (i % 3 == 0 && i != 0) tmpNumber = "," + tmpNumber

		tmpNumber = number.substring(number.length - i - 1, number.length - i) + tmpNumber
	}

	var resNumber = sign + tmpNumber

	if (dot) resNumber += fraction

	return resNumber
}


//check whether passed validated control contains any of passes invalid characters (case insensitive)
function invalidChars(validatedControl, validatedName, charString, msg) {
	var validatedString = trim(validatedControl.value).toLowerCase()
	charString = charString.toLowerCase()

	for (var i=0; i<validatedString.length; i++) {
		for (var j=0; j<charString.length; j++) {
			if (validatedString.charAt(i) == charString.charAt(j)) {

				if (typeof (Common_Scripts_FuncLib_Space) == "undefined")
					Common_Scripts_FuncLib_Space = "space"

				var alertString = charString.split("").join("  ")
				alertString = alertString.split("     ").join("  " + Common_Scripts_FuncLib_Space + "  ")

				if (msg == null)
					msg = validatedName + " cannot contain any of the following illegal characters:\n\n " + alertString

				msg = msg.replace(/\[IllegalCharacters\]/, alertString)
				alert(msg)
				validatedControl.focus()
				return true
			}
		}
	}

	return false
}


//check whether text-box is empty
function isEmpty(field, fieldName, msg) {
	if (trim(field.value) == "") {
		if (msg == null)
			msg = "Please enter " + fieldName + "."

		alert(msg)
		field.focus()
		return true
	}

	return false
}


//check whether selection has been made in the select-box
function notSelected(field, fieldName, msg) {
	if (field.selectedIndex == 0) {
		if (msg == null)
			msg = "Please select " + fieldName + "."

		alert(msg)
		field.focus()
		return true
	}

	return false
}


//trim string
function trim(stringToTrim) {
	var i, j

	//left trim
	for(i=0; i<stringToTrim.length; i++) {
		if (stringToTrim.charAt(i) != " ") break
	}

	//right trim
	for(j=stringToTrim.length-1; j>=i; j--) {
		if (stringToTrim.charAt(j) != " ") break
	}

	return stringToTrim.substring(i, j + 1)
}


//validate email address
function notEmail(field, fieldName, msg) {
	var email = trim(field.value)

	if (email == "") return false

	var at = false
	var dot = false

	for (var i=0; i<email.length; i++) {
		if (email.charAt(i) == "@") at = true
		if (email.charAt(i) == "." && at) dot = true
	}

	if (!(at && dot && email.length > 5)) {
		if (msg == null)
			msg = "The " + fieldName + " you entered is not a valid e-mail address."

		alert(msg)
		field.focus()
		field.select()
		return true
	}

	//check for invalid characters
	if (invalidChars(field, "E-mail", '%,;|" <>\\/')) return true

	return false
}


//validate ASCII Character Set
function charCheck(field, message, toASCIIfield) {
	if (toASCIIfield == null)
		var txt = field.value
	else
		var txt = ToASCII(field)

	for (var i=0; i<txt.length; i++) {
		if (txt.charCodeAt(i) >= 128) {
			alert(message)
			field.focus()
			return true
		}
	}

	return false
}


//checks whether selected option has invalid value and selects option with gotoIndex
function checkInvalVal(sel, invalVal, gotoIndex) {
	if (GetSelVal(sel) == invalVal) sel.selectedIndex = gotoIndex
}


//date validation
function validDate(date, ctlDay) {
	return date.getDate() == GetSelVal(ctlDay)
}


//set select box value
function SetSelVal(ctl, newVal) {
	for (var i=0; i<ctl.length; i++) {
		if (ctl[i].value == newVal) {
			ctl[i].selected = true
			ctl.selectedIndex = i
			break
		}
	}
}


//get select box value
function GetSelVal(ctl) {
	var selIdx = ctl.selectedIndex
	return selIdx == -1 ? "" : ctl[selIdx].value
}


//get select box text
function GetSelText(ctl) {
	var selIdx = ctl.selectedIndex
	return selIdx == -1 ? "" : ctl[selIdx].text
}


//check length of text in the passed control
function invalLength(ctl, ctlName, maxLen, msg) {
	var ctlLen = trim(ctl.value).length

	if (ctlLen > maxLen) {
		if (msg == null)
			msg = "Please limit " + ctlName + " to " + maxLen + " characters. You have used " + ctlLen + " characters."

		msg = msg.replace(/\[MaxCharactersAllowed\]/, maxLen)
		msg = msg.replace(/\[NumOfCharacters\]/, ctlLen)
		
		alert(msg)
		ctl.focus()
		return true
	}

	return false
}


//open new window
function openWnd(url, name, height, width, directories, location, menubar, resizable, scrollbars, status, toolbar) {
	wnd = window.open(url, name, "alwaysRaised=1,height=" + height + ",width=" + width + ",directories=" + directories + ",locaton=" + location + ",menubar=" + menubar + ",resizable=" + resizable + ",scrollbars=" + scrollbars + ",status=" + status + ",toolbar=" + toolbar)
	wnd.focus()
}


//unencode url-encoded string
function URLDecode(urlStr) {
	return unescape(urlStr.replace(/\+/g, " "))
}


//remove all name/value pairs with the passed name from url-encoded querystring
function remQStringName(qString, name) {
	var i
	var qStringNew = ""

	if (qString != "") {
		var curName
		var arrNameVal = qString.split("&")

		for (i in arrNameVal) {
			curName = URLDecode(arrNameVal[i].split("=")[0])
			if (curName.toLowerCase() != name.toLowerCase()) qStringNew += "&" + arrNameVal[i]
		}
	}

	return qStringNew.substr(1)
}


//replace or add name/value pairs in url-encoded querystring
function setQStringName(qString, name, arrVal) {
	var qStringNew = remQStringName(qString, name)
	var start = qStringNew == "" ? 1 : 0
	for (var i in arrVal) qStringNew += "&" + escape(name) + "=" + escape(arrVal[i])
	return qStringNew.substr(start)
}


//extract (first!) value from querystring for the passed name
function GetQSVal(qsName) {
	var qsPair
	var qString = location.search.substr(1)
	var arrNameVal = qString.split("&")

	for (var i in arrNameVal) {
		qsPair = arrNameVal[i].split("=")
		if (URLDecode(qsPair[0]).toLowerCase() == qsName.toLowerCase()) return URLDecode(qsPair[1])
	}

	return ""
}


//convert character to ASCII
function ToASCII(field){
	var txt = field.value

	for (var i = 0; i<txt.length; i++) {
		switch (txt.charAt(i)){
			case "\u00E1": case "\u00E0": case "\u00E2": case "\u00E3": case "\u00E5": case "\u0105":
				txt = txt.replace(txt.charAt(i), "a")
				break
			case "\u00E4": case "\u00E6":
				txt = txt.replace(txt.charAt(i), "ae")
				break
			case "\u0107": case "\u00E7":
				txt = txt.replace(txt.charAt(i), "c")
				break
			case "\u00F0":
				txt = txt.replace(txt.charAt(i), "d")
				break
			case "\u00E9": case "\u00E8": case "\u00EA": case "\u00EB": case "\u0119":
				txt = txt.replace(txt.charAt(i), "e")
				break
			case "\u00ED": case "\u00EC": case "\u00EE": case "\u00EF":
				txt = txt.replace(txt.charAt(i), "i")
				break
			case "\u0142":
				txt = txt.replace(txt.charAt(i), "l")
				break
			case "\u0144": case "\u00F1":
				txt = txt.replace(txt.charAt(i), "n")
				break
			case "\u00F3": case "\u00F2": case "\u00F4": case "\u00F5": case "\u00F8":
				txt = txt.replace(txt.charAt(i), "o")
				break
			case "\u00F6": case "\u0153":
				txt = txt.replace(txt.charAt(i), "oe")
				break
			case "\u015B":
				txt = txt.replace(txt.charAt(i), "s")
				break
			case "\u00FA": case "\u00F9": case "\u00FB":
				txt = txt.replace(txt.charAt(i), "u")
				break
			case "\u00FC":
				txt = txt.replace(txt.charAt(i), "ue")
				break
			case "\u00FD": case "\u00FF":
				txt = txt.replace(txt.charAt(i), "y")
				break
			case "\u017A": case "\u017C":
				txt = txt.replace(txt.charAt(i), "z")
				break
			case "\u00C1": case "\u00C0": case "\u00C2": case "\u00C3": case "\u00C5": case "\u0104":
				txt = txt.replace(txt.charAt(i), "A")
				break
			case "\u00C6": case "\u00C4":
				txt = txt.replace(txt.charAt(i), "Ae")
				break
			case "\u0106": case "\u00C7":
				txt = txt.replace(txt.charAt(i), "C")
				break
			case "\u00D0":
				txt = txt.replace(txt.charAt(i), "D")
				break
			case "\u00C9": case "\u00C8": case "\u00CA": case "\u00CB": case "\u0118":
				txt = txt.replace(txt.charAt(i), "E")
				break
			case "\u00CD": case "\u00CC": case "\u00CE": case "\u00CF":
				txt = txt.replace(txt.charAt(i), "I")
				break
			case "\u0141":
				txt = txt.replace(txt.charAt(i), "L")
				break
			case "\u0143": case "\u00D1":
				txt = txt.replace(txt.charAt(i), "N")
				break
			case "\u00D3": case "\u00D2": case "\u00D4": case "\u00D5": case "\u00D8":
				txt = txt.replace(txt.charAt(i), "O")
				break
			case "\u00D6": case "\u0152":
				txt = txt.replace(txt.charAt(i), "Oe")
				break
			case "\u015A":
				txt = txt.replace(txt.charAt(i), "S")
				break
			case "\u00DF":
				txt = txt.replace(txt.charAt(i), "ss")
				break
			case "\u00DA": case "\u00D9": case "\u00DB":
				txt = txt.replace(txt.charAt(i), "U")
				break
			case "\u00DC":
				txt = txt.replace(txt.charAt(i), "Ue")
				break
			case "\u00DD": case "\u0178":
				txt = txt.replace(txt.charAt(i), "Y")
				break
			case "\u0179": case "\u017B":
				txt = txt.replace(txt.charAt(i), "Z")
				break
			default:
				break
		}
	}
	return txt
}
